using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace LeisureCenter.MetadataHandlers
{
    class MetadataHandlersManager
    {
        private List<MetadataHandlerFactory> readers = new List<MetadataHandlerFactory>();

        public MetadataHandlersManager()
        {
            this.readers.Add(new OggMetadataHandlerFactory());
            this.readers.Add(new Mp3MetadataHandlerFactory());
            this.readers.Add(new VideoDefaultMetadataHandlerFactory());
        }

        public MetadataHandlerFactory GetAppropriateMetaDataHandler(string fileName, Stream stream)
        {
            foreach (MetadataHandlerFactory mdh in this.readers)
            {
                stream.Position = 0;
                if (mdh.AcceptMedia(fileName, stream))
                {
                    return mdh;
                }
            }
            return null;
        }

        public MetadataHandlerFactory GetAppropriateMetaDataHandler(string filename)
        {
            try
            {
                using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    return GetAppropriateMetaDataHandler(filename, fs);
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }
    }
}
